//
//  LMBaseTableView.m
//  LMLabelSelect
//
//  Created by LM on 2022/12/1.
//

#import "LMBaseTableView.h"

@interface LMBaseTableView()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation LMBaseTableView

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupDefault];
    }
    return self;
}
-(id) initWithFrame:(CGRect)frame andStyle:(UITableViewStyle) style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setupDefault];
    }
    return self;
}

-(void) setupDefault
{
    //    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 ){
    //        self.rowHeight = UITableViewAutomaticDimension;
    //        self.estimatedRowHeight = 44;
    //    }
    
    self.estimatedRowHeight = 0;
//    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;

    //    self.rowHeight = 44;
    __weak typeof (self) weakself = self;
    self.delegate = weakself;
    self.dataSource = weakself;
    
//    self.tableFooterView = [[UIView alloc] init];

    self.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];


    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
//#ifdef __IPHONE_9_0
//    if (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0) {
//        self.cellLayoutMarginsFollowReadableWidth = false;
//    }
//#endif
//    if ([UIDevice currentDevice].systemVersion.floatValue >= 9) {
//        self.cellLayoutMarginsFollowReadableWidth = false;
//    }
    
     
    if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
        [self setSeparatorInset: UIEdgeInsetsZero];
    }
    if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
        [self setLayoutMargins: UIEdgeInsetsZero];
    }

    self.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
   
    
//    self.rowHeight = UITableViewAutomaticDimension;
}

//-(CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 ){
//        return self.estimatedRowHeight;
//    }
//    return 44;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_heightForRowBlock) {
        return _heightForRowBlock(tableView, indexPath);
    }
    return self.rowHeight;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_displayCell) {
        return _displayCell(tableView, cell, indexPath);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (_heightForSectionHeaderBlock) {
        CGFloat height = _heightForSectionHeaderBlock(tableView, section);
        if (height > 0) {
            return height;
        }
        return 0.1;
    }
    return self.sectionHeaderHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (_HeightForSectionFooterBlock) {
        CGFloat height = _HeightForSectionFooterBlock(tableView, section);
        if (height > 0) {
            return height;
        }
        return 0.1;
    }
    return self.sectionFooterHeight;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_numberOfSectionsBlock) {
        return _numberOfSectionsBlock(tableView);
    }
    else if(_sections > 0){
        return _sections;
    }
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_numberOfRowsBlock) {
        return _numberOfRowsBlock(tableView, section);
    }
    
    return _rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    if (_cellForRowBlock) {
        return _cellForRowBlock(tableView, indexPath);
    }
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_selectBlock) {
        _selectBlock(tableView, indexPath);
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_deselectBlock) {
        _deselectBlock(tableView, indexPath);
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (_viewForSectionFooterBlock) {
        UIView *view = _viewForSectionFooterBlock(tableView, section);
        return view;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (_viewForSectionHeaderBlock) {
        UIView *view = _viewForSectionHeaderBlock(tableView, section);
        return view;
    }
    return nil;
}

// 是否可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 编辑模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _selectEditingStyle;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    if (_scrollViewDidScrollToTop) {
        _scrollViewDidScrollToTop(scrollView);
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_scrollViewDidScroll) {
        _scrollViewDidScroll(scrollView);
    }
    if (self.isNeedControlKeFu) {
//        [[KefuWindowButton shared] tgHidden];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{//滑动结束
    if (_scrollViewDidEndDecelerating) {
        _scrollViewDidEndDecelerating(scrollView);
    }
    BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
    if (scrollToScrollStop) {
//        [[KefuWindowButton shared] tgShow];
    }
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    if (_scrollViewWillBeginDecelerating) {
        
        _scrollViewWillBeginDecelerating(scrollView);
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (_scrollViewWillBeginDragging) {
        _scrollViewWillBeginDragging(scrollView);
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (_scrollViewDidEndDragging) {
        _scrollViewDidEndDragging(scrollView,decelerate);
    }
    if (!decelerate) {
        // 停止类型3
        BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
        if (dragToDragStop) {
//            [[KefuWindowButton shared] tgShow];
        }
    }
}

@end
